from selenium.webdriver import ActionChains
from hyrobot.common import *
import pyautogui
import pytest
import time
import json
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.by import By
from lib.webui_permission import *

class c73259b:
    name = '产品管理员查看自己创建产品的申请记录，可编辑、删除自己创建的产品-c73259b'  # 测试用例名字

    # 初始化方法
    def setup(self):
        STEP(1, '登陆网站')
        driver = open_browser()
        productor1_login(driver)

    # 清除方法
    def teardown(self):
        db = kylin_connect()
        # 使用cursor()方法获取操作游标
        INFO(3)
        cursor = db.cursor()
        # SQL 更新语句
        sql = "UPDATE product_info SET is_deleted = 0 WHERE product_name = 'NAME--34'"
        try:
            # 执行SQL语句
            cursor.execute(sql)
            INFO(4)
            # 提交到数据库执行
            db.commit()
            INFO(5)
        except:
            # 发生错误时回滚
            db.rollback()
            INFO(6)
        # 关闭数据库连接
        db.close()
        driver = get_global_webdriver()
        driver.quit()

    # 测试用例步骤
    def teststeps(self):
        # 获取webdriver对象 对应 已经登录好的浏览器
        driver = get_global_webdriver()
        # driver = webdriver.Firefox()

        extending_button = driver.find_element_by_id("hamburger-container")
        try:
            driver.find_element(By.CLASS_NAME, "el-tooltip")
            extending_button.click()
        except:
            INFO("左侧栏没有隐藏")
        # click product manage
        time.sleep(1)
        product_manage_word2 = driver.find_element(By.XPATH,
                                                   "/html/body/div/div/div[1]/ul/div[3]/a/li/span"
                                                   )
        product_manage_word2.click()
        time.sleep(1)
        top_title2 = driver.find_element(By.XPATH,
                                         "/html/body/div/div/div[2]/div/div/div[2]/span/span[2]/span[1]/span"
                                         ).text
        CHECK_POINT("上方第3个标题对应", "产品管理" == top_title2)

        STEP(2, "点击查看")
        time.sleep(1)
        input_product_name = driver.find_element(By.CSS_SELECTOR,
                                                 "div.el-form-item:nth-child(1) > div:nth-child(2) > div:nth-child(1) > input:nth-child(1)"
                                                 )
        input_product_name.clear()
        time.sleep(1)
        input_product_name.send_keys('NAME--34')
        time.sleep(1)
        # click select
        driver.find_element(By.CSS_SELECTOR,
                            'div.el-form-item:nth-child(3) > div:nth-child(1) > button:nth-child(1) > span:nth-child(1)'
                            ).click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR,
                            'tr.el-table__row:nth-child(1) > td:nth-child(7) > div:nth-child(1) > button:nth-child(1)'
                            ).click()
        time.sleep(1)
        input_box = driver.find_elements(By.CLASS_NAME, "el-input__inner")
        input_box[0].clear()
        input_box[0].send_keys("xiangmu")
        # INFO(input_box[0].get_attribute('value'))
        applyer = "editor3"
        input_box[1].clear()
        input_box[1].send_keys(applyer)
        # click select
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR,
                            "button.el-button--primary:nth-child(1) > span:nth-child(1)"
                            ).click()
        got_applyer = driver.find_element(By.CSS_SELECTOR,
                                          "tr.el-table__row:nth-child(1) > td:nth-child(4) > div:nth-child(1)"
                                          ).text
        CHECK_POINT("检查申请人是否正确", got_applyer == applyer)
        # check detail
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR,
                            "tr.el-table__row:nth-child(1) > td:nth-child(11) > div:nth-child(1) > button:nth-child(1) > span:nth-child(1)"
                            ).click()
        time.sleep(1)
        got_title = driver.find_element(By.CSS_SELECTOR,
                                        "div.el-dialog__wrapper:nth-child(4) > div:nth-child(1) > div:nth-child(1) > span:nth-child(1)"
                                        ).text
        CHECK_POINT("内部标题是否一致", got_title == "申请记录信息")
        time.sleep(1)
        # close
        driver.find_element(By.CSS_SELECTOR,
                            "div.el-dialog__footer:nth-child(3) > div:nth-child(1) > button:nth-child(1) > span:nth-child(1)"
                            ).click()
        # 回去产品管理
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR,
                            "span.el-breadcrumb__item:nth-child(2) > span:nth-child(1) > a:nth-child(1)"
                            ).click()

        STEP(3, "进行编辑")
        # TODO 修改加载时间
        time.sleep(3)
        driver.find_element(By.CSS_SELECTOR,
                                "tr.el-table__row:nth-child(1) > td:nth-child(7) > div:nth-child(1) > button:nth-child(2) > span:nth-child(1)"
                                ).click()
        time.sleep(2)
        got_edit_words = edit_product(driver)
        # may need check edit which check detail
        STEP(4,"去到产品列表")
        time.sleep(1)
        product_word2 = driver.find_element(By.XPATH,
                                            "/html/body/div/div/div[1]/ul/div[1]/a/li/span"
                                            )
        product_word2.click()
        time.sleep(1)
        top_title0 = driver.find_element(By.XPATH,
                                         "/html/body/div/div/div[2]/div/div/div[2]/span/span/span[1]/span"
                                         ).text
        CHECK_POINT("上方第1个标题对应", "产品列表" == top_title0)

        STEP(4,"比较修改后的产品描述")
        # 在方法里修改成 exchanged words.
        got_words_in_pro_list = driver.find_element(By.CSS_SELECTOR,
                                                    ".el-card__body > ul:nth-child(2) > li:nth-child(1) > div:nth-child(1) > div:nth-child(1) > div:nth-child(2) > div:nth-child(5)"
                                                    ).text
        CHECK_POINT("比较编辑后是否修改描述",got_words_in_pro_list == got_edit_words)

        STEP(5,"去产品管理")
        time.sleep(1)
        product_manage_word2 = driver.find_element(By.XPATH,
                                                   "/html/body/div/div/div[1]/ul/div[3]/a/li/span"
                                                   )
        product_manage_word2.click()
        time.sleep(1)
        top_title2 = driver.find_element(By.XPATH,
                                         "/html/body/div/div/div[2]/div/div/div[2]/span/span[2]/span[1]/span"
                                         ).text
        CHECK_POINT("上方第3个标题对应", "产品管理" == top_title2)

        # delete
        STEP(4, "删除")
        # 删除后要恢复
        # may need use pymysql to update the status
        time.sleep(1)
        input_product_name = driver.find_element(By.CSS_SELECTOR,
                                                 "div.el-form-item:nth-child(1) > div:nth-child(2) > div:nth-child(1) > input:nth-child(1)"
                                                 )
        input_product_name.clear()
        time.sleep(1)
        input_product_name.send_keys('NAME--34')
        time.sleep(1)
        # click select
        driver.find_element(By.CSS_SELECTOR,
                            'div.el-form-item:nth-child(3) > div:nth-child(1) > button:nth-child(1) > span:nth-child(1)'
                            ).click()
        time.sleep(3)
        got_deleted_product_name = driver.find_element(By.CSS_SELECTOR,
                                                       "tr.el-table__row:nth-child(1) > td:nth-child(1) > div:nth-child(1)"
                                                       ).text
        INFO(got_deleted_product_name)
        driver.find_element(By.CSS_SELECTOR,
                            "tr.el-table__row:nth-child(1) > td:nth-child(7) > div:nth-child(1) > button:nth-child(3) > span:nth-child(1)"
                            ).click()
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR,
                            "button.el-button--small:nth-child(2)"
                            ).click()
        time.sleep(1)
        select_delete_product_input = driver.find_element(By.CSS_SELECTOR,
                                                          "div.el-form-item:nth-child(1) > div:nth-child(2) > div:nth-child(1) > input:nth-child(1)"
                                                          )
        select_delete_product_input.send_keys(got_deleted_product_name)
        # select
        time.sleep(1)
        driver.find_element(By.CSS_SELECTOR,
                            "div.el-form-item:nth-child(3) > div:nth-child(1) > button:nth-child(1) > span:nth-child(1)"
                            ).click()
        time.sleep(1)
        empty_text = driver.find_element(By.CSS_SELECTOR,
                                         ".el-table__empty-text"
                                         ).text
        CHECK_POINT("检查提示是否是暂无数据",empty_text == '暂无数据')